<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <multiInput class="w-50" v-model="form.adjust_in_order_no" placeholder="调拨单"></multiInput>
        <multiInput class="w-50" v-model="form.sku" placeholder="SKU"></multiInput>
        <VGroup>
          <el-select style="max-width: 100px" v-model="form.personRange.type" placeholder="请选择">
            <el-option v-for="item in personOptions" :key="item.value" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
          <select-personnel class="w-50" v-model="form.personRange.value"></select-personnel>
        </VGroup>
        <el-select class="selectClass w-30" v-model="form.adjust_in_status" placeholder="状态">
          <el-option v-for="item in options['stockin.adjustInOrder']?.[0].adjustInOrderStatus" :key="item.value"
            :label="item.label" :value="item.value"></el-option>
        </el-select>
        <SelectWarehouse class="w-50" v-model="form.out_warehouse_id" :multiple="true" placeholder="发货仓库"></SelectWarehouse>
        <SelectWarehouse class="w-50" v-model="form.in_warehouse_id" :multiple="true" placeholder="目的仓库"></SelectWarehouse>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 100px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()" style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button class="ml-auto" @click="handleExport"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" @query="query"></ComModal>
    <derived-center ref="DerivedCenterRef"></derived-center>

  </VPage>
</template>
<script setup name="StorageAdjust-store" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryAdjustList } from './api'
import ComModal from './components/ComModal.vue'
import multiInput from '@/components/multiInput';
import SelectWarehouse from "@/components/SelectWarehouse"

const timeOption = [
  { label: "创建时间", value: "create_time" },
]

const personOptions = [
  { label: "领取人", value: "op_user_id" },
  { label: "取消人", value: "cancel_user_id" },
  { label: "创建人", value: "create_user_id" },
  { label: "出库人", value: "out_warehouse_user_id" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 60 },
  formConfig: {
    data: {
      shop_id: '',
      create_time: '',
      status: '',
      operations_personnel: '',
      personRange: { type: 'create_user_id', value: [], range: true },
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'adjust_in_order_no', title: '调拨入库单', width: 220, slots: {
        default: ({ row }) => {
          return <div class="base-text-color cursor-pointer" onClick={() => open('view',row.id)}> {row.adjust_in_order_no} </div>
        }
      }
    },
    {
      field: 'out_warehouse_name', title: '调出仓', width: 160
    },
    {
      field: 'in_warehouse_name', title: '调入仓', width: 160
    },
    {
      field: 'adjust_in_status_cn', title: '状态', width: 120
    },
    {
      field: 'sku_count', title: 'SKU数量', width: 120
    },
    {
      field: 'sku_num', title: 'SKU件数', width: 120
    },
    {
      field: 'create_user', title: '创建人', width: 160
    },
    {
      field: 'create_time', title: '创建日期', width: 185
    },
    {
      field: 'upper_time', title: '送上架时间', width: 185
    },
    {
      field: 'out_warehouse_time', title: '出库时间', width: 185
    },
    {
      title: '操作', width: 70, fixed: "right", slots: {
        default: ({ row }) => {
          return <div class="table-option">
            <div>
              {row.adjust_in_status == 1 && <el-link type="primary" underline={false} class="mr-2" onClick={() => open('edit', row.id)}>收货</el-link>}
            </div>
          </div >
        }
      }
    }
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryAdjustList({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
})
const { options } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "stockin.adjustInOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()

// 查看
const ComModalRef = ref()
const open = (type, id) => {
  ComModalRef.value.open(type, id)
}

// 导出
const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 46,
    tag_name: 'wms',
    condition
  })
}


</script>